import { random } from '../common/math.js';

let _elem;
let _animation;
let _isDestroy = false
let _delayTimer;
let _isEnemyMove = false


export const framesEnemy = [
  {
    src: "/assets/images/bat1.png",
  },
  {
    src: "/assets/images/bat2.png",
  },
  {
    src: "/assets/images/bat1.png",
  },
  {
    src: "/assets/images/bat3.png",
  },
]

export const initEnemy = (elem) => {
  _elem = elem
  
}

export const startEnemyFly = () => {
  _elem.start()
  
}

export const stopEnemyFly = () => {
  _elem.stop()
}

export const createEnemyPos = () => {
  const boderWidth = 40
  const Rmax = 233 - boderWidth;
  const Rmin = 190;
  let y = random(boderWidth, 310)
   if (process.env.NODE_ENV == 'development') {
    // y = boderWidth
    return [233, 40]
  }
  const Ox = 233;
  const Oy = 233;
  const x1 = -Math.sqrt(Math.pow(Rmax, 2) - Math.pow((y - Oy), 2)) + Ox
  let x2, x3;
  if (Math.sqrt(Math.pow(Rmin, 2) - Math.pow((y - Oy), 2))) {
     x2 = -Math.sqrt(Math.pow(Rmin, 2) - Math.pow((y - Oy), 2)) + Ox
     x3 = Math.sqrt(Math.pow(Rmin, 2) - Math.pow((y - Oy), 2)) + Ox
  }
  const x4 = Math.sqrt(Math.pow(Rmax, 2) - Math.pow((y - Oy), 2)) + Ox
  if (x2 && x3) {
    console.log({ y, x1, x2, x3, x4, xp: (x1 + x4) / 2, xp2: (x2 + x3) / 2 })
    return [Date.now() % 2 ? random(x1, x2) : random(x3, x4), y]
  }
  // console.log({ y, x1, x4 })
  return [random(x1, x4), y]
}
let _timer;
export const destroyEnemy = (cb) => {
  _isDestroy = true
  _animation?.cancel()
  const keyframes = [
    {
      transformOrigin: "50% 50%",
      time: 0,
      transform: {
        // rotate: "0deg",
        scale: '1'
      },
    },
    {
      transformOrigin: "50% 50%",
      time: 100,
      transform: {
        // rotate: "180deg",
        scale: '0.2'
      },
    }
  ]
  const options = {
    duration: 700,
    easing: 'linear',
    delay: 0,
    // fill: 'forwards'
  }
  _animation = _elem.animate(keyframes, options)
  _animation.play()
  clearTimeout(_timer)
  _timer = setTimeout(() => {
    cb && cb()
  }, 500)
  // _animation.onfinish = () => {
  //   cb && cb()
  // }
}

// 内存释放
export const releaseEnemy = () => {
  clearTimeout(_timer)
  clearTimeout(_delayTimer)
  _animation?.cancel()
}

/**
 * 敌人是否移动
 * @returns 
 */
export const isEnemyMove = () => {
  return _isEnemyMove
}


export const moveEnemy = (delay = 6000, targetX, targetY, cb) => {
  _isDestroy = false
  const centerX = 233;
  const centerY = 233;
  const keyframes = [
    {
      transformOrigin: "50% 50%",
      time: 0,
      transform: {
        translateY: 0,
        translateX: 0,
      },
    },
    {
      transformOrigin: "50% 50%",
      time: 100,
      transform: {
        translateY: centerY - targetY,
        translateX: centerX - targetX,
      },
    }
  ]
  const options = {
    duration: 1300,
    easing: 'linear',
    delay: 0,
    fill: 'forwards'
  }
  _animation = _elem.animate(keyframes, options)
  clearTimeout(_delayTimer)
  _isEnemyMove = false
  _delayTimer = setTimeout(() => {
    _isEnemyMove = true
    _animation.play()
  }, delay)
  _animation.onfinish = () => {
    _isEnemyMove = false
    if (!_isDestroy) {
      cb()
    }
  }
}
